package com.mallcloud.mall.product.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.common.xss.core.XssCleanIgnore;
import com.mallcloud.mall.product.api.dto.SpuDTO;
import com.mallcloud.mall.product.api.dto.SpuPublishDTO;
import com.mallcloud.mall.product.api.entity.SpuModel;
import com.mallcloud.mall.product.api.vo.SpuVO;
import com.mallcloud.mall.product.service.SpuService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "spu信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/spu")
public class SpuController {
    private final SpuService spuService;

    @XssCleanIgnore
    @ApiOperation("发布商品")
    @PostMapping("/publish")
    @PreAuthorize("@pms.hasPermission('product_spu_add')")
    public R<Boolean> publish(@Validated @RequestBody SpuPublishDTO dto) {
        return R.ok(spuService.publish(dto));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_spu_view')")
    public R<IPage<SpuVO>> page(Page page, SpuModel dto) {
        return R.ok(spuService.pageSpu(page, dto));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_spu_view')")
    public R<List<SpuModel>> list(SpuModel dto) {
        return R.ok(spuService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_spu_view')")
    public R<SpuModel> getInfo(SpuModel dto) {
        return R.ok(spuService.getById(dto.getSpuId()));
    }

    @XssCleanIgnore
    @ApiOperation("查询商品详情")
    @GetMapping("/getDetail")
    @PreAuthorize("@pms.hasPermission('product_spu_view')")
    public R<String> getDetail(SpuModel dto) {
        return R.ok(spuService.getDetail(dto.getSpuId()));
    }

    @XssCleanIgnore
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_spu_add')")
    public R<Boolean> save(@RequestBody SpuDTO dto) {
        return R.ok(spuService.saveSpu(dto));
    }

    @XssCleanIgnore
    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_spu_edit')")
    public R<Boolean> updateById(@RequestBody SpuDTO dto) {
        return R.ok(spuService.updateSpu(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{spuId}")
    @PreAuthorize("@pms.hasPermission('product_spu_del')")
    public R<Boolean> removeById(@PathVariable Integer spuId) {
        return R.ok(spuService.removeById(spuId));
    }

    @ApiOperation("批量删除spu信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_spu_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> spuIds) {
        return R.ok(spuService.removeByIds(spuIds));
    }
}
